package com.atguigu.redis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GoodController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${server.port}")
    private String serverPort;
    @GetMapping("/buy_goods")
    public String buy_goods(){
      String result =   stringRedisTemplate.opsForValue().get("goods:001");
      int goodNumber  = result ==null?0:Integer.parseInt(result);
      if (goodNumber>0){
          int realNumber = goodNumber -1;
          stringRedisTemplate.opsForValue().set("goods:001",String.valueOf(realNumber));
          return "成功购买商品,库存还剩下："+realNumber+"件"+"提供端口"+serverPort;
      }else{
          return "卖完了";
      }
    }

}
